package whu.edu.demo.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import whu.edu.demo.domain.Supplier;
import whu.edu.demo.exception.ProductException;
import whu.edu.demo.service.SupplierService;

@RestController
@RequestMapping("/supplier")
public class SupplierController {
    @Autowired
    SupplierService supplierService;


    @GetMapping("/{id}")
    public ResponseEntity<Supplier> getSupplier(@PathVariable long id){
        Supplier result = supplierService.getById(id);
        if(result==null) {
            return ResponseEntity.noContent().build();
        }else{
            return ResponseEntity.ok(result);
        }
    }

    @PostMapping("")
    public Supplier addProduct(@RequestBody Supplier supplier) throws ProductException {
        supplierService.saveOrUpdate(supplier);
        return supplier;
    }

    @PutMapping("/{id}")
    public void updateProduct(@PathVariable long id,@RequestBody Supplier supplier) throws ProductException {
        supplierService.saveOrUpdate(supplier);
    }

    @DeleteMapping("/{id}")
    public void deleteProduct(@PathVariable long id){
        supplierService.removeById(id);
    }


}


